package com.photoeditor.demo.model.bean;

import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.text.Layout.Alignment;
import android.text.StaticLayout;
import android.text.TextPaint;

import com.common.base.ComponentContext;
import com.common.base.utils.DeviceUtils;
import com.photoeditor.R;
import com.photoeditor.demo.model.image.collage.util.MathUtil;
import com.photoeditor.demo.model.image.util.MODEL;
import com.photoeditor.demo.util.font.FontHelper;

public class EditTextBean {
	
	//默认的高度
	//private static int DEFAULT_HEIGHT = DeviceUtils.dip2px(ComponentContext.getContext(), 60);
	
	//默认的宽度
	//private static int DEFAULT_WIDTH = DEFAULT_HEIGHT;
	
	/**
	 * 按钮的大小
	 */
	public final static int RADIUS = ComponentContext.getContext().getResources().getDimensionPixelSize(R.dimen.image_edit_operation_button_size);
	
	//文字的TextPaint
	private TextPaint mTextPaint;
	
	//文字的Rect
	private RectF mTextRect;
	
	//View的Rect
	private RectF mViewRect;
	
	//按钮的Rect
	private RectF mButtonRect;
	private RectF mDeleteRect;

	//文字
	private String mString;

	private int mCheckId;

	private int mStyle;

	private String mFontKey = FontHelper.DEFAULT_FONT_PKG_NAME;
	
	//显示文字的区域layout
	private StaticLayout mLayout;
	
	/**
	 * 是否正被点击
	 */
	private boolean mIsTouch = false;
	
	//文字和框的上下左右的padding
	private float mPadding = DeviceUtils.dip2px(ComponentContext.getContext(), 5);
	
	/**
	 * 当前旋转的角度
	 */
	private float mDegree = 0;
	
	/**
	 * 用于点击位置的映射
	 */
	private Matrix mMatrix ;
	
	/**
	 * 是否需要保存 用于标识
	 */
	private boolean mIsNeedSave = false;
	
	/**
	 * 默认阴影的颜色
	 */
	private static final int DEFAULT_COLOR = Color.rgb(0x00, 0x00, 0x00);
	
	//是不是粗体
	private boolean mIsBold = true;
	
	//是不是有阴影
	private boolean mIsShadow = false;

	private Paint mBgPaint;
	
	/**
	 * 一些阴影相关的参数
	 */
	public final static int SHADOW_DX = DeviceUtils.dip2px(ComponentContext.getContext(), 0);
	
	public final static int SHADOW_DY = DeviceUtils.dip2px(ComponentContext.getContext(), 0);
	
	public final static int SHADOW_RADIUS = DeviceUtils.dip2px(ComponentContext.getContext(), 5);

	/**
	 * 
	 * @param tp
	 * @param e
	 * @param m
	 * @param mDrawableRect
	 */
	public EditTextBean(TextPaint tp, String e, MODEL m, RectF mDrawableRect){
		if(e != null){
			mTextPaint = tp;
			mTextPaint.setFakeBoldText(true);
			mString = e;
			mTextRect = new RectF();
			mViewRect = new RectF();
			mButtonRect = new RectF();
			mDeleteRect = new RectF();
			mMatrix = new Matrix();
			initBgPaint();
			initData(m, mDrawableRect);
		}
	}

	private void initBgPaint() {
		mBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mBgPaint.setStyle(Paint.Style.FILL);
		mBgPaint.setColor(Color.TRANSPARENT);
	}

	public EditTextBean(TextPaint tp, String e, float pointX, float pointY){
		if(e != null){
			mTextPaint = tp;
			mTextPaint.setFakeBoldText(true);
			mString = e;
			mTextRect = new RectF();
			mViewRect = new RectF();
			mButtonRect = new RectF();
			mDeleteRect = new RectF();
			mMatrix = new Matrix();
			initBgPaint();
			initData(pointX, pointY);
		}
	}

	/**
	 * 初始化数据
	 * @param x
	 * @param y
	 */
	public void initData(float x, float y){
	
//		String str[] = mString.split("\n");
//		int len = str.length;
		
		float width = 0, height;
//		for(int i = 0 ; i < len ; i++){
//			float w = mTextPaint.measureText(str[i]);
//			width = Math.max(width, w);
//		}
//		
//		mLayout = new StaticLayout(mString, mTextPaint, (int)(width + 100f), Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
		
		width = StaticLayout.getDesiredWidth(mString, mTextPaint);
		mLayout = new StaticLayout(mString, mTextPaint, (int)(width + 1f), Alignment.ALIGN_OPPOSITE, 1.0f, 0.0f, false);
		height = mLayout.getHeight();
		
		mTextRect.left = x - width / 2 - mPadding;
		mTextRect.top =  y - height / 2 - mPadding;
		mTextRect.right = mTextRect.left + width + 2 * mPadding;
		mTextRect.bottom = mTextRect.top + height + 2 * mPadding;
		
		/*float w = mTextRect.width();
		float h = mTextRect.height();
		if(w < DEFAULT_WIDTH || h < DEFAULT_HEIGHT){
			float dw,dh;
			if(DEFAULT_WIDTH - w <= DEFAULT_HEIGHT - h){
				dh = DEFAULT_HEIGHT;
				dw = (DEFAULT_HEIGHT - h) + w;
			} else{
				dw = DEFAULT_WIDTH;
				dh = DEFAULT_WIDTH - w + h;
			}
			mViewRect.left = mTextRect.left + mTextRect.width() / 2 - dw / 2;
			mViewRect.top = mTextRect.top + mTextRect.height() / 2 - dh / 2;
			mViewRect.right = mViewRect.left + dw;
			mViewRect.bottom = mViewRect.top + dh;
		} else{
			mViewRect.left = mTextRect.left;
			mViewRect.right = mTextRect.right;
			mViewRect.top = mTextRect.top;
			mViewRect.bottom = mTextRect.bottom;
		}*/

		mViewRect.left = mTextRect.left;
		mViewRect.right = mTextRect.right;
		mViewRect.top = mTextRect.top;
		mViewRect.bottom = mTextRect.bottom;
		
		mButtonRect.left = mViewRect.right - RADIUS;
		mButtonRect.right = mViewRect.right + RADIUS;
		mButtonRect.top = mViewRect.bottom - RADIUS;
		mButtonRect.bottom = mViewRect.bottom + RADIUS;

		mDeleteRect.left = mViewRect.left - RADIUS;
		mDeleteRect.right = mViewRect.left + RADIUS;
		mDeleteRect.top = mViewRect.top - RADIUS;
		mDeleteRect.bottom = mViewRect.top + RADIUS;

	}

	public Paint getBgPaint() {
		return mBgPaint;
	}

	void updateBgColor(int color){
		if(mBgPaint != null) {
			mBgPaint.setColor(color);
		}
	}

	/**
	 * 初始化数据
	 * @param m
	 * @param mDrawableRect
	 */
	public void initData(MODEL m, RectF mDrawableRect){
	
//		String str[] = mString.split("\n");
//		int len = str.length;
		
		float width = 0, height;
//		for(int i = 0 ; i < len ; i++){
//			float w = mTextPaint.measureText(str[i]);
//			width = Math.max(width, w);
//		}
//		
//		mLayout = new StaticLayout(mString, mTextPaint, (int)(width + 100f), Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
		
		width = StaticLayout.getDesiredWidth(mString, mTextPaint);
		mLayout = new StaticLayout(mString, mTextPaint, (int)(width + 1f), Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
		height = mLayout.getHeight();
		
		int ord = m.ordinal();
		if(ord == 0){//center
			mTextRect.left = mDrawableRect.left + mDrawableRect.width() / 2 - width / 2 - mPadding;
			mTextRect.top =  mDrawableRect.top + mDrawableRect.height() / 2 - height / 2 - mPadding;
			mTextRect.right = mTextRect.left + width + 2 * mPadding;
			mTextRect.bottom = mTextRect.top + height + 2 * mPadding;
		} else if(ord == 1){//TOP_CENTER
			mTextRect.left = mDrawableRect.left + mDrawableRect.width() / 2 - width / 2 - mPadding;
			mTextRect.top = mDrawableRect.top + height - mPadding + mDrawableRect.height() / 10f;
			mTextRect.top = Math.min(mTextRect.top, mDrawableRect.top + mDrawableRect.height() / 2 - height / 2 - mPadding);
			mTextRect.right = mTextRect.left + width + 2 * mPadding;
			mTextRect.bottom = mTextRect.top + height + 2 * mPadding;
		} else if(ord == 2){//LEFT_CENTER
			mTextRect.left = mDrawableRect.left - (width / 2 - (mDrawableRect.width() / 10f + height / 2f)) - mPadding;
			mTextRect.top = mDrawableRect.top + mDrawableRect.height() / 2 - height / 2 - mPadding;
			mTextRect.left = Math.min(mTextRect.left, mDrawableRect.left + mDrawableRect.width() / 2 - width / 2 - mPadding);
			mTextRect.right = mTextRect.left + width + 2 * mPadding;
			mTextRect.bottom = mTextRect.top + height + 2 * mPadding;
		} else if(ord == 3){//RIGHT_CENTER
			mTextRect.left = mDrawableRect.right - (width / 2 + (mDrawableRect.width() / 10f + height / 2f)) - mPadding;
			mTextRect.top = mDrawableRect.top + mDrawableRect.height() / 2 - height / 2 - mPadding;
			mTextRect.top = Math.max(mTextRect.top, mDrawableRect.top + mDrawableRect.height() / 2 - height / 2 - mPadding);
			mTextRect.right = mTextRect.left + width + 2 * mPadding;
			mTextRect.bottom = mTextRect.top + height + 2 * mPadding;
		} else if(ord == 4){//BOTTOM_CENTER
			mTextRect.left = mDrawableRect.left + mDrawableRect.width() / 2 - width / 2 - mPadding;
			mTextRect.top = mDrawableRect.bottom - height - mPadding - mDrawableRect.height() / 10f;
			mTextRect.top = Math.max(mTextRect.top, mDrawableRect.top + mDrawableRect.height() / 2 - height / 2 - mPadding);
			mTextRect.right = mTextRect.left + width + 2 * mPadding;
			mTextRect.bottom = mTextRect.top + height + 2 * mPadding;
		}
		/*float w = mTextRect.width();
		float h = mTextRect.height();
		if(w < DEFAULT_WIDTH || h < DEFAULT_HEIGHT){
			float dw,dh;
			if(DEFAULT_WIDTH - w <= DEFAULT_HEIGHT - h){
				dh = DEFAULT_HEIGHT;
				dw = (DEFAULT_HEIGHT - h) + w;
			} else{
				dw = DEFAULT_WIDTH;
				dh = DEFAULT_WIDTH - w + h;
			}
			mViewRect.left = mTextRect.left + mTextRect.width() / 2 - dw / 2;
			mViewRect.top = mTextRect.top + mTextRect.height() / 2 - dh / 2;
			mViewRect.right = mViewRect.left + dw;
			mViewRect.bottom = mViewRect.top + dh;
		} else{
			mViewRect.left = mTextRect.left;
			mViewRect.right = mTextRect.right;
			mViewRect.top = mTextRect.top;
			mViewRect.bottom = mTextRect.bottom;
		}*/

		mViewRect.left = mTextRect.left;
		mViewRect.right = mTextRect.right;
		mViewRect.top = mTextRect.top;
		mViewRect.bottom = mTextRect.bottom;
		
		mButtonRect.left = mViewRect.right - RADIUS;
		mButtonRect.right = mViewRect.right + RADIUS;
		mButtonRect.top = mViewRect.bottom - RADIUS;
		mButtonRect.bottom = mViewRect.bottom + RADIUS;

		mDeleteRect.left = mViewRect.left - RADIUS;
		mDeleteRect.right = mViewRect.left + RADIUS;
		mDeleteRect.top = mViewRect.top - RADIUS;
		mDeleteRect.bottom = mViewRect.top + RADIUS;

		if(ord == 2){
			setDegree(-90);
		} else if(ord == 3){
			setDegree(90);
		}
	}
	
	/**
	 * 更新String
	 * @param s
	 */
	public void updateString(String s){
		mString = s;
		doUpdate(true);
	}
	
	/**
	 * 更新color
	 * @param color
	 */
	public void updateColor(int color){
		mTextPaint.setColor(color);
		doUpdate(false);
	}

	public void updateFont(Typeface typeface, String fontKey) {
		mTextPaint.setTypeface(typeface);
		mFontKey = fontKey;
		doUpdate(true);
	}

	public void updateStyle(int style) {
		mStyle = style;
	}

	public int getStyle() {
		return mStyle;
	}

	public void updateCheckId(int checkId) {
		if (checkId >= 0) {
			mCheckId = checkId;
		}
	}

	public int getCheckId() {
		return mCheckId;
	}
	
	/**
	 * 设置Bold
	 * @param isBold
	 */
	public void updateBold(boolean isBold){
		mIsBold = isBold;
		mTextPaint.setFakeBoldText(isBold);
		doUpdate(false);
	}
	
	/**
	 * 设置阴影
	 * @param isShader
	 */
	public void updateShadow(boolean isShader){
		mIsShadow = isShader;
		if(isShader){
			mTextPaint.setShadowLayer(SHADOW_RADIUS, SHADOW_DX, SHADOW_DY, DEFAULT_COLOR);
		} else{
			mTextPaint.setShadowLayer(0, SHADOW_DX, SHADOW_DY, DEFAULT_COLOR);
		}
		doUpdate(false);
	}
	
	private void doUpdate(boolean isRectChange){
//		String str[] = mString.split("\n");
//		int len = str.length;
		
		if(!isRectChange){
			mLayout = new StaticLayout(mString, mTextPaint, mLayout.getWidth(), Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
		} else{
			float width = 0, height;
	//		for(int i = 0 ; i < len ; i++){
	//			float w = mTextPaint.measureText(str[i]);
	//			width = Math.max(width, w);
	//		}
			
			width = StaticLayout.getDesiredWidth(mString, mTextPaint);
			mLayout = new StaticLayout(mString, mTextPaint, (int)(width + 1f), Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
	
			height = mLayout.getHeight();
			
			float centerX = mTextRect.centerX();
			float centerY = mTextRect.centerY();
			
			mTextRect.left = centerX - width / 2 - mPadding;
			mTextRect.top =  centerY - height / 2 - mPadding;
			mTextRect.right = mTextRect.left + width + 2 * mPadding;
			mTextRect.bottom = mTextRect.top + height + 2 * mPadding;
			
			/*float w = mTextRect.width();
			float h = mTextRect.height();
			if(w < DEFAULT_WIDTH || h < DEFAULT_HEIGHT){
				float dw,dh;
				if(DEFAULT_WIDTH - w <= DEFAULT_HEIGHT - h){
					dh = DEFAULT_HEIGHT;
					dw = (DEFAULT_HEIGHT - h) + w;
				} else{
					dw = DEFAULT_WIDTH;
					dh = DEFAULT_WIDTH - w + h;
				}
				mViewRect.left = mTextRect.left + mTextRect.width() / 2 - dw / 2;
				mViewRect.top = mTextRect.top + mTextRect.height() / 2 - dh / 2;
				mViewRect.right = mViewRect.left + dw;
				mViewRect.bottom = mViewRect.top + dh;
			} else{
				mViewRect.left = mTextRect.left;
				mViewRect.right = mTextRect.right;
				mViewRect.top = mTextRect.top;
				mViewRect.bottom = mTextRect.bottom;
			}*/

			mViewRect.left = mTextRect.left;
			mViewRect.right = mTextRect.right;
			mViewRect.top = mTextRect.top;
			mViewRect.bottom = mTextRect.bottom;
			
			mButtonRect.left = mViewRect.right - RADIUS;
			mButtonRect.right = mViewRect.right + RADIUS;
			mButtonRect.top = mViewRect.bottom - RADIUS;
			mButtonRect.bottom = mViewRect.bottom + RADIUS;

			mDeleteRect.left = mViewRect.left - RADIUS;
			mDeleteRect.right = mViewRect.left + RADIUS;
			mDeleteRect.top = mViewRect.top - RADIUS;
			mDeleteRect.bottom = mViewRect.top + RADIUS;
		}
	}
	
	/**
	 * 旋转
	 * @param degree
	 */
	public void setDegree(float degree) {
		this.mDegree += degree;
	}
	
	/**
	 * 处理旋转和缩放
	 * @param downX
	 * @param downY
	 * @param nowX
	 * @param nowY
	 * @param type
	 */
	public void dealScaleAndRotation(float downX, float downY, float nowX, float nowY, boolean type){
		float px = mViewRect.centerX();
		float py = mViewRect.centerY();
		
		float x1 = downX - px;
		float y1 = downY - py;
		
		float x2 = nowX - px;
		float y2 = nowY - py;
		
		float scale  = (float)(Math.sqrt(x2 * x2 + y2 * y2) / Math.sqrt(x1 * x1 + y1 * y1));
		
		if(mViewRect.width() > mTextRect.width() && scale > 1.0f && type){
			float d1 = (float)Math.sqrt((mTextRect.width() / 2) * (mTextRect.width() / 2) + (mTextRect.height() / 2) * (mTextRect.height() / 2));
			float dx = nowX - downX;
			float dy = nowY - downY;
			float d2 = (float)Math.sqrt(dx * dx + dy * dy) + d1;
			scale = d2 / d1;
		}
		
		double degree1 = MathUtil.getDegree(px, py, downX, downY, px + 1, py);
		
		double degree2 = MathUtil.getDegree(px, py, nowX, nowY, px + 1, py);
		
		int location1 = MathUtil.getLocation(x1, y1);
		int location2 = MathUtil.getLocation(x2, y2);
		
		if(location2 == 1 || location2 == 2){
			degree2 = -degree2;
		}
		
		if(location1 == 1 || location1 == 2){
			degree1 = -degree1;
		}
		
		double degree = degree2 - degree1;
	
		setScale(scale);
		setDegree((float)degree);
	}
	
	/**
	 * 放大缩小
	 * @param scale
	 */
	public void setScale(float scale) {
		
		mPadding = mPadding * scale;
		
		Matrix m = new Matrix();
		m.setScale(scale, scale, mTextRect.centerX(), mTextRect.centerY());
		
		RectF rect = new RectF();
		m.mapRect(rect, mTextRect);
		
		mTextRect = rect;
		
		
		mTextPaint.setTextSize(mTextPaint.getTextSize() * scale);
		
		float width = StaticLayout.getDesiredWidth(mString, mTextPaint);
		mLayout = new StaticLayout(mString, mTextPaint, (int)(width + 1f), Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
		
		float centerX = mTextRect.centerX();
		
		/**
		 * 这个的处理是为了防止宽越界
		 */
		mTextRect.left = centerX - width / 2 - mPadding;
		mTextRect.right = mTextRect.left + width + 2 * mPadding;

		
	/*	if(scale >= 1.0f){
//			if(mTextRect.contains(mViewRect) || (!mTextRect.contains(mViewRect) && !mViewRect.contains(mTextRect))){
//				mViewRect.left = mTextRect.left;
//				mViewRect.right = mTextRect.right;
//				mViewRect.top = mTextRect.top;
//				mViewRect.bottom = mTextRect.bottom;
//			} else{
			float w = mTextRect.width();
			float h = mTextRect.height();
			if(w < DEFAULT_WIDTH || h < DEFAULT_HEIGHT){
				float dw,dh;
				if(DEFAULT_WIDTH - w <= DEFAULT_HEIGHT - h){
					dh = DEFAULT_HEIGHT;
					dw = (DEFAULT_HEIGHT - h) + w;
				} else{
					dw = DEFAULT_WIDTH;
					dh = DEFAULT_WIDTH - w + h;
				}
				mViewRect.left = mTextRect.left + mTextRect.width() / 2 - dw / 2;
				mViewRect.top = mTextRect.top + mTextRect.height() / 2 - dh / 2;
				mViewRect.right = mViewRect.left + dw;
				mViewRect.bottom = mViewRect.top + dh;
			} else{
				mViewRect.left = mTextRect.left;
				mViewRect.right = mTextRect.right;
				mViewRect.top = mTextRect.top;
				mViewRect.bottom = mTextRect.bottom;
			}
//			}
			mButtonRect.left = mViewRect.right - RADIUS;
			mButtonRect.right = mViewRect.right + RADIUS;
			mButtonRect.top = mViewRect.bottom - RADIUS;
			mButtonRect.bottom = mViewRect.bottom + RADIUS;
		} else{//缩小
			float w = mTextRect.width();
			float h = mTextRect.height();
			if(w < DEFAULT_WIDTH || h < DEFAULT_HEIGHT){
				float dw,dh;
				if(DEFAULT_WIDTH - w <= DEFAULT_HEIGHT - h){
					dh = DEFAULT_HEIGHT;
					dw = (DEFAULT_HEIGHT - h) + w;
				} else{
					dw = DEFAULT_WIDTH;
					dh = DEFAULT_WIDTH - w + h;
				}
				mViewRect.left = mTextRect.left + mTextRect.width() / 2 - dw / 2;
				mViewRect.top = mTextRect.top + mTextRect.height() / 2 - dh / 2;
				mViewRect.right = mViewRect.left + dw;
				mViewRect.bottom = mViewRect.top + dh;
			} else{
				mViewRect.left = mTextRect.left;
				mViewRect.right = mTextRect.right;
				mViewRect.top = mTextRect.top;
				mViewRect.bottom = mTextRect.bottom;
			}

			mButtonRect.left = mViewRect.right - RADIUS;
			mButtonRect.right = mViewRect.right + RADIUS;
			mButtonRect.top = mViewRect.bottom - RADIUS;
			mButtonRect.bottom = mViewRect.bottom + RADIUS;
		}*/

		mViewRect.left = mTextRect.left;
		mViewRect.right = mTextRect.right;
		mViewRect.top = mTextRect.top;
		mViewRect.bottom = mTextRect.bottom;

		mButtonRect.left = mViewRect.right - RADIUS;
		mButtonRect.right = mViewRect.right + RADIUS;
		mButtonRect.top = mViewRect.bottom - RADIUS;
		mButtonRect.bottom = mViewRect.bottom + RADIUS;


		mDeleteRect.left = mViewRect.left - RADIUS;
		mDeleteRect.right = mViewRect.left + RADIUS;
		mDeleteRect.top = mViewRect.top - RADIUS;
		mDeleteRect.bottom = mViewRect.top + RADIUS;

	}

	public void move(float dx, float dy){
		mViewRect.offset(dx, dy);
		mTextRect.offset(dx, dy);
		mButtonRect.offset(dx, dy);
		mDeleteRect.offset(dx, dy);
	}
	
	public float getPadding() {
		return mPadding;
	}
	
	public float getmDegree() {
		return mDegree;
	}

	public Matrix getMatrix(){
		mMatrix.setRotate(-mDegree, mTextRect.centerX(), mTextRect.centerY());
		return mMatrix;
	}
	
	public RectF getmTextRect() {
		return mTextRect;
	}


	public RectF getmViewRect() {
		return mViewRect;
	}

	public RectF getmButtonRect() {
		return mButtonRect;
	}

	public RectF getDeleteRect() {
		return mDeleteRect;
	}

	//	public RectF getmEditRect(){
//		return mEditRect;
//	}
	
	public boolean isBold(){
		return mIsBold;
	}

	public boolean isShadow(){
		return mIsShadow;
	}
	
	public int getColor(){
		return mTextPaint.getColor();
	}
	
	public String getmString() {
		return mString;
	}

	public StaticLayout getmLayout() {
		return mLayout;
	}

	public TextPaint getPaint() {
		return mTextPaint;
	}


	public boolean isTouch() {
		return mIsTouch;
	}

	public boolean isNeedSave() {
		return mIsNeedSave;
	}

	public void setIsTouch(boolean mIsTouch) {
		this.mIsTouch = mIsTouch;
	}
	
	public void setIsNeedSave(boolean isNeedSave) {
		this.mIsNeedSave = isNeedSave;
	}


	private Matrix mLastMatrix;

	/**
	 * 用于缩放时的处理
	 * @param baseRect  是图片的真实Rect
	 * @param supperRect  是显示的真实Rect
	 * @param matrix  是ImageView的ImageMatrix
	 * @param cacheMatrix  一个用于操作的缓存Matrix
	 * @param viewRect ImageView当前的Rect
	 */
	public void doScaleMove(RectF baseRect, RectF supperRect, Matrix matrix, Matrix cacheMatrix, RectF viewRect){
		if(mLastMatrix == null) {
			RectF dstRect = new RectF();
			RectF a = new RectF(mTextRect);
			a.offset(-viewRect.left, -viewRect.top);

			//防止坐标对他的影响
			RectF supperRect1 = new RectF(supperRect);
			supperRect1.offset(-viewRect.left, -viewRect.top);
			//aScale是 baseRect -> supperRect的缩放值
			float bScale = supperRect1.width() / baseRect.width();
			Matrix m = new Matrix();
			m.postScale(bScale, bScale, baseRect.centerX(), baseRect.centerY());
			m.postTranslate(supperRect1.centerX() - baseRect.centerX(), supperRect1.centerY() - baseRect.centerY());
			m.invert(m);

			//matrixA是去掉 从baseRect转换到supperRect后的Matrix
			Matrix matrixA = new Matrix(matrix);
			matrixA.preConcat(m);
			matrixA.mapRect(dstRect, a);

			float scale = dstRect.width() / a.width();
			float dx = dstRect.centerX() - a.centerX();
			float dy = dstRect.centerY() - a.centerY();

			cacheMatrix.reset();
			cacheMatrix.setScale(scale, scale, a.centerX(), a.centerY());
			cacheMatrix.mapRect(dstRect, a);

			dstRect.offset(viewRect.left, viewRect.top);
			dstRect.offset(dx, dy);

			doScaleMove(dstRect, scale);

			mLastMatrix = new Matrix();
			mLastMatrix.set(matrix);
		} else{
			RectF dstRect = new RectF();
			RectF a = new RectF(mTextRect);
			a.offset(-viewRect.left, -viewRect.top);

			mLastMatrix.invert(mLastMatrix);
			//matrixA是去掉 从baseRect转换到supperRect后的Matrix
			Matrix matrixA = new Matrix(matrix);
			matrixA.preConcat(mLastMatrix);
			matrixA.mapRect(dstRect, a);

			float scale = dstRect.width() / a.width();
			float dx = dstRect.centerX() - a.centerX();
			float dy = dstRect.centerY() - a.centerY();

			cacheMatrix.reset();
			cacheMatrix.setScale(scale, scale, a.centerX(), a.centerY());
			cacheMatrix.mapRect(dstRect, a);

			dstRect.offset(viewRect.left, viewRect.top);
			dstRect.offset(dx, dy);

			doScaleMove(dstRect, scale);

			mLastMatrix.set(matrix);
		}
	}

	/**
	 * 设置当前的Matrix
	 * @param matrix
	 */
	public void setLastMatrix(Matrix matrix){
		if(mLastMatrix == null) {
			mLastMatrix = new Matrix();
		}
		mLastMatrix.set(matrix);
	}


	private void doScaleMove(RectF dst, float scale){
		mTextRect = dst;
		mPadding = mPadding * scale;
		mTextPaint.setTextSize(mTextPaint.getTextSize() * scale);
		float width = StaticLayout.getDesiredWidth(mString, mTextPaint);
		mLayout = new StaticLayout(mString, mTextPaint, (int)(width + 1f), Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);

		float centerX = mTextRect.centerX();

		/**
		 * 这个的处理是为了防止宽越界
		 */
		mTextRect.left = centerX - width / 2 - mPadding;
		mTextRect.right = mTextRect.left + width + 2 * mPadding;

		/*if(scale >= 1.0f){
			float w = mTextRect.width();
			float h = mTextRect.height();
			if(w < DEFAULT_WIDTH || h < DEFAULT_HEIGHT){
				float dw,dh;
				if(DEFAULT_WIDTH - w <= DEFAULT_HEIGHT - h){
					dh = DEFAULT_HEIGHT;
					dw = (DEFAULT_HEIGHT - h) + w;
				} else{
					dw = DEFAULT_WIDTH;
					dh = DEFAULT_WIDTH - w + h;
				}
				mViewRect.left = mTextRect.left + mTextRect.width() / 2 - dw / 2;
				mViewRect.top = mTextRect.top + mTextRect.height() / 2 - dh / 2;
				mViewRect.right = mViewRect.left + dw;
				mViewRect.bottom = mViewRect.top + dh;
			} else{
				mViewRect.left = mTextRect.left;
				mViewRect.right = mTextRect.right;
				mViewRect.top = mTextRect.top;
				mViewRect.bottom = mTextRect.bottom;
			}
			mButtonRect.left = mViewRect.right - RADIUS;
			mButtonRect.right = mViewRect.right + RADIUS;
			mButtonRect.top = mViewRect.bottom - RADIUS;
			mButtonRect.bottom = mViewRect.bottom + RADIUS;
		} else{//缩小
			float w = mTextRect.width();
			float h = mTextRect.height();
			if(w < DEFAULT_WIDTH || h < DEFAULT_HEIGHT){
				float dw,dh;
				if(DEFAULT_WIDTH - w <= DEFAULT_HEIGHT - h){
					dh = DEFAULT_HEIGHT;
					dw = (DEFAULT_HEIGHT - h) + w;
				} else{
					dw = DEFAULT_WIDTH;
					dh = DEFAULT_WIDTH - w + h;
				}
				mViewRect.left = mTextRect.left + mTextRect.width() / 2 - dw / 2;
				mViewRect.top = mTextRect.top + mTextRect.height() / 2 - dh / 2;
				mViewRect.right = mViewRect.left + dw;
				mViewRect.bottom = mViewRect.top + dh;
			} else{
				mViewRect.left = mTextRect.left;
				mViewRect.right = mTextRect.right;
				mViewRect.top = mTextRect.top;
				mViewRect.bottom = mTextRect.bottom;
			}
			mButtonRect.left = mViewRect.right - RADIUS;
			mButtonRect.right = mViewRect.right + RADIUS;
			mButtonRect.top = mViewRect.bottom - RADIUS;
			mButtonRect.bottom = mViewRect.bottom + RADIUS;
		}*/

		mViewRect.left = mTextRect.left;
		mViewRect.right = mTextRect.right;
		mViewRect.top = mTextRect.top;
		mViewRect.bottom = mTextRect.bottom;

		mButtonRect.left = mViewRect.right - RADIUS;
		mButtonRect.right = mViewRect.right + RADIUS;
		mButtonRect.top = mViewRect.bottom - RADIUS;
		mButtonRect.bottom = mViewRect.bottom + RADIUS;

		mDeleteRect.left = mViewRect.left - RADIUS;
		mDeleteRect.right = mViewRect.left + RADIUS;
		mDeleteRect.top = mViewRect.top - RADIUS;
		mDeleteRect.bottom = mViewRect.top + RADIUS;
	}

	public String getFontKey() {
		return mFontKey;
	}
}
